package zhihui.mobile.ui.shop.attention;

import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zgm.utils.ui.UiTextHeader;

import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.SpuInfo;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.shop.BaseShopPresenter;
import zhihui.mobile.presenter.shop.ShopAttentionPresenter;
import zhihui.mobile.ui.BaseActivity;

/**
 * Created by 张公铭 on 2017/8/14 0014.
 */

public class ShopAttentionActivity extends BaseActivity implements IShopAttentionContract.IShopAttentionView
{
    private ShopAttentionPresenter attentionPresenter = new ShopAttentionPresenter();
    private BaseShopPresenter baseShopPresenter = new BaseShopPresenter();

    private UiTextHeader mheader;
    private PullToRefreshListView lv_content;
    private Button btn_cancel;
    private LinearLayout ll_normal;
    private Button btn_sales;

    private ShopAttentionAdapter adapter;

    @Override
    public void initView()
    {
        mheader = (UiTextHeader) findViewById(R.id.shop_attention_header);
        mheader.setTitle("我的关注");
        mheader.setLeftBottonIcon(R.drawable.shop_back);
        mheader.setLeftButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                goBack(ShopAttentionActivity.class);
            }
        });

        mheader.setRightText("编辑");
        mheader.setRightTextListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (((TextView) v).getText().toString().equals("编辑"))
                {
                    mheader.setRightText("完成");
                    btn_cancel.setVisibility(View.VISIBLE);
                    //刷新列表。显示复选框
                    adapter.setEdit(true);
                    adapter.notifyDataSetChanged();
                } else
                {
                    mheader.setRightText("编辑");
                    btn_cancel.setVisibility(View.GONE);
                    //刷新列表，隐藏复选框
                    attentionPresenter.initViewData();
                }
            }
        });

        lv_content = (PullToRefreshListView) findViewById(R.id.lv_shop_attention_content);
        adapter = new ShopAttentionAdapter(this);
        adapter.setListener(new ShopAttentionAdapter.IOnCheckListener()
        {
            @Override
            public void check(int spuId)
            {
                attentionPresenter.addCheck(spuId);
            }

            @Override
            public void unCheck(int spuId)
            {
                attentionPresenter.deleteCheck(spuId);
            }
        });
        lv_content.setAdapter(adapter);
        lv_content.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                baseShopPresenter.goShopDetails(ShopAttentionActivity.this, (int) adapter.getItemId(position-1), 0,ShopAttentionActivity.this);
            }
        });

        btn_cancel = (Button) findViewById(R.id.btn_shop_attention_cancel);
        btn_cancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                attentionPresenter.cancelAttention();
            }
        });
        btn_cancel.setVisibility(View.GONE);

        ll_normal = (LinearLayout) findViewById(R.id.ll_shop_attention_normal);
        ll_normal.setVisibility(View.VISIBLE);

        btn_sales = (Button) findViewById(R.id.btn_shop_attention_go_sales);
        btn_sales.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                attentionPresenter.goSalesActivity(ShopAttentionActivity.this, ShopAttentionActivity.this, 1);
            }
        });

        attentionPresenter.initViewData();
    }

    @Override
    protected int getLayoutResId()
    {
        return R.layout.shop_attention_activity;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{attentionPresenter};
    }

    @Override
    protected void onInitPresenter()
    {
        attentionPresenter.init(this, this);
    }

    @Override
    public void refreshView(List<SpuInfo> list)
    {
        if (list != null && list.size() > 0)
        {
            btn_cancel.setVisibility(View.VISIBLE);
            ll_normal.setVisibility(View.GONE);
            adapter.setList(list);
            adapter.setEdit(false);
            adapter.notifyDataSetChanged();
        } else
        {
            btn_cancel.setVisibility(View.INVISIBLE);
            ll_normal.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onRefreshComplete()
    {
        lv_content.onRefreshComplete();
    }
}
